<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\exam\model\admin\QuestionDifficult as QuestionDifficultModel;

/**
 * 试题难度逻辑层
 */
class QuestionDifficult extends QuestionDifficultModel
{
    /**
     * 获取列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/3
     *
     * @param array $map
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map = [])
    {
        return $this->where($map)->order('create_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * 创建试题难度
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/3
     *
     * @param array $data
     * @return $this
     */
    public function createDifficult(array $data)
    {
        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($data);

        $save = [
            'mhm_id' => request()->mhm_id,
            'title'  => $data['title'],
        ];

        // 创建成功
        if (!$this->save($save)) {
            throw new LogicException('创建失败');
        }

        Log::write('新建试题难度【' . $data['title'] . '】', 'exam', 'create', true);

        // 返回值
        return $this;
    }

    /**
     * 编辑试题难度
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/3
     *
     * @param $id
     * @param $data
     * @return array|\think\Model
     */
    public function editDifficult($id, $data)
    {
        $difficult = $this->where('id', $id)->findOrFail();

        $check       = $data;
        $check['id'] = $id;
        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($check);

        $difficult->title = $data['title'];

        if (!$difficult->save()) {
            throw new LogicException('修改失败');
        }

        Log::write('更新试题难度【' . $data['title'] . '】', 'exam', 'update', true);

        return $difficult;
    }

    /**
     * 删除试题难度
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/3
     *
     * @param int $id
     */
    public function deleteDifficult(int $id)
    {
        $difficult = $this->where('id', $id)->findOrFail();

        if (!$difficult->delete()) {
            throw new LogicException("删除失败");
        }
        Log::write('删除试题难度【' . $difficult->title . '】', 'exam', 'delete', true);
    }

    /**
     * 难易度选项数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/23
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cascader()
    {
        $cate = $this->field('id as value,title')->select();

        return $cate->toArray();
    }

    /**
     * 查询难易度,当不存在的时候会创建
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/29
     *
     * @param string $difficult
     * @return array
     */
    public function findOrCreate(string $difficult)
    {
        if (!$difficult) {
            return [];
        }
        static $loaded;
        if (isset($loaded[$difficult])) {
            return $loaded[$difficult];
        }

        // 查询知识点分类是否存在
        $difficultId = $this->where('title', $difficult)->value('id');
        if (!$difficultId) {
            // 创建分类
            $model        = new static();
            $model->title = $difficult;
            $model->save();
            $difficultId        = $model->getLastInsID();
            $loaded[$difficult] = $difficultId;
        }

        return $difficultId;
    }
}